package com.ltxtiyu.video.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.ltxtiyu.video.cacheDao.ConfigCacheDao;
import com.ltxtiyu.video.common.ConfigConstant;
import com.ltxtiyu.video.common.JsonTool;
import com.ltxtiyu.video.common.RetEnum;
import com.ltxtiyu.video.dao.ltx.MusicDao;
import com.ltxtiyu.video.entity.Music;
import com.ltxtiyu.video.service.app.MusicService;
import com.ltxtiyu.video.tool.AliTool;
import com.ltxtiyu.video.tool.ObjectTool;
import org.apache.commons.lang3.StringUtils;
import org.hashids.Hashids;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 音乐 服务
 */
@Service
public class MusicServiceImpl implements MusicService {
    public static final Logger LOGGER = LoggerFactory.getLogger(MusicServiceImpl.class);
    @Autowired
    ConfigCacheDao configCacheDao;
    @Autowired
    MusicDao musicDao;

    /**
     * 查看music信息
     * @param params
     * @return
     */
    @Override
    public String musicDetail(Map<String, String> params) {
        String hashMusicId = ObjectTool.stringOrNull(params.get("musicId"));
        if(hashMusicId == null){
            return JsonTool.genJson(RetEnum.REQUEST_PARAM_ERROR);
        }

        Integer musicId = -1;
        try {
            Hashids hashids = new Hashids(ConfigConstant.HASH_IDS_SECRET_KEY,ConfigConstant.HASH_IDS_VALUE_LENGTH);
            musicId = new Long(hashids.decode(hashMusicId)[0]).intValue();
        }catch (Exception e){
            LOGGER.error(">>>>>>>>>>>>>>>>hashids解密发生错误,请求参数:{},错误信息:{}",hashMusicId,e);
        }

        Music music = musicDao.selectByPrimaryKey(musicId);
        if(music == null || music.getStatus().intValue() != 1){
            return JsonTool.genJson(RetEnum.MUSIC_IS_NOT_EXIST);    // 该音乐不存在
        }

        String playUrl = "";
        if(StringUtils.isNotBlank(music.getMusicId())){
            // 读取获取图片的相关配置
            String ACCESS_ID = configCacheDao.selectByCkey(ConfigConstant.ACCESS_ID).getCvalue();
            String ACCESS_KEY = configCacheDao.selectByCkey(ConfigConstant.ACCESS_KEY).getCvalue();
            String BUCKET_ID_MUSIC = configCacheDao.selectByCkey(ConfigConstant.BUCKET_ID_MUSIC).getCvalue();
            String OSS_CDN_MUSIC = configCacheDao.selectByCkey(ConfigConstant.OSS_CDN_MUSIC).getCvalue();     // 查看音乐
            // 查看音乐的alitool
            AliTool aliShowTool = AliTool.getInstance(OSS_CDN_MUSIC, ACCESS_ID, ACCESS_KEY, BUCKET_ID_MUSIC);
            // 获取music播放地址
            playUrl = aliShowTool.getAccessURL(music.getMusicId());
        }

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> resultMusic = new HashMap<>();


        resultMusic.put("id",hashMusicId);              // 加密过的
        resultMusic.put("mid",music.getId());           // 原始id
        resultMusic.put("title",music.getTitle());      // 歌曲名称
        resultMusic.put("artist",music.getArtist());    // 歌手
        resultMusic.put("playUrl",playUrl);             // 播放地址
        result.put("music",resultMusic);
        return JsonTool.genJson(RetEnum.SUCCESS,result);
    }

    /**
     * 音乐集合
     * @param params
     * @return
     */
    @Override
    public String musicList(Map<String, String> params) {
        Integer start = Integer.parseInt(params.get("start"));
        Integer rows = Integer.parseInt(params.get("rows"));

        // 查看可用的所有歌曲
        Map<String, Object> page = new HashMap<>();
        page.put("start",start);
        page.put("rows",rows);
        List<Music> musicList = musicDao.selctAllWithActivated(page);


        // 读取获取图片的相关配置
        /*String ACCESS_ID = configCacheDao.selectByCkey(ConfigConstant.ACCESS_ID).getCvalue();
        String ACCESS_KEY = configCacheDao.selectByCkey(ConfigConstant.ACCESS_KEY).getCvalue();
        String BUCKET_ID_MUSIC = configCacheDao.selectByCkey(ConfigConstant.BUCKET_ID_MUSIC).getCvalue();
        String OSS_CDN_MUSIC = configCacheDao.selectByCkey(ConfigConstant.OSS_CDN_MUSIC).getCvalue();     // 查看音乐
        // 查看音乐的alitool
        AliTool aliShowTool = AliTool.getInstance(OSS_CDN_MUSIC, ACCESS_ID, ACCESS_KEY, BUCKET_ID_MUSIC);*/

        List<Map<String, Object>> resultMusicList = new ArrayList<>();
        Hashids hashids = new Hashids(ConfigConstant.HASH_IDS_SECRET_KEY,ConfigConstant.HASH_IDS_VALUE_LENGTH);
        for(Music music : musicList){
            Map<String, Object> resultMusic = new HashMap<>();
            /*String playUrl = "";
            if(StringUtils.isNotBlank(music.getMusicId())){
                // 获取music播放地址
                playUrl = aliShowTool.getAccessURL(music.getMusicId());
            }*/
            resultMusic.put("id",hashids.encode(music.getId()));    // 加密过的id
            resultMusic.put("mid",music.getId());           // 原始id
            resultMusic.put("title",music.getTitle());      // 歌曲名称
            resultMusic.put("artist",music.getArtist());    // 歌手
//            resultMusic.put("playUrl",playUrl);             // 播放地址
            resultMusicList.add(resultMusic);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("musicList",resultMusicList);
        return JsonTool.genJson(RetEnum.SUCCESS,result);
    }
}
